<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

// 当前路由路径
const currentPath = computed(() => route.path)

// 导航项
const navItems = [
  { path: '/', name: '首页', icon: '🏠' },
  { path: '/routes', name: '线路推荐', icon: '🚌' },
  { path: '/news', name: '旅游资讯', icon: '📰' },
  { path: '/cart', name: '购物车', icon: '🛒' },
  { path: '/user', name: '个人中心', icon: '👤' }
]

// 登录状态
const isLoggedIn = ref(false)
const username = ref('')

// 检查登录状态
const checkLoginStatus = () => {
  const storedUser = localStorage.getItem('user')
  if (storedUser) {
    const user = JSON.parse(storedUser)
    isLoggedIn.value = true
    username.value = user.nickname || '用户'
  } else {
    isLoggedIn.value = false
    username.value = ''
  }
}

// 登录
const login = () => {
  router.push('/login')
}

// 退出登录
const logout = () => {
  localStorage.removeItem('user')
  checkLoginStatus() // 更新状态
  router.push('/') // 可选：跳转到首页
}

// 组件挂载时检查登录状态
onMounted(() => {
  checkLoginStatus()
  // 监听 localStorage 变化，以便在其他标签页登录/登出时同步状态
  window.addEventListener('storage', checkLoginStatus);
})

</script>

<template>
  <nav class="navbar">
    <div class="nav-container">
      <div class="logo">
        <router-link to="/" data-text="泉州文旅">泉州文旅</router-link>
      </div>
      <div class="nav-links">
        <router-link 
          v-for="item in navItems" 
          :key="item.path" 
          :to="item.path"
          :class="['nav-item', { active: currentPath === item.path }]"
        >
          <span class="nav-icon">{{ item.icon }}</span>
          <span class="nav-text">{{ item.name }}</span>
        </router-link>
      </div>
      <div class="auth-section">
        <div v-if="isLoggedIn" class="user-info">
          <span class="username">欢迎, {{ username }}</span>
          <button @click="logout" class="auth-button logout-button">退出登录</button>
        </div>
        <div v-else>
          <button @click="login" class="auth-button login-button">请登录</button>
        </div>
      </div>
    </div>
  </nav>
</template>

<style scoped>
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.navbar {
  position: sticky;
  top: 0;
  width: 100%;
  background: linear-gradient(90deg, #f8f0e3, #e0d8c3); /* 淡雅的米色/土黄色渐变 */
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); /* 柔和的阴影 */
  z-index: 1000;
  padding: 0 20px;
  border-bottom: 1px solid #d3cbbd; /* 柔和的底部边框 */
  font-family: 'Noto Serif SC', serif; /* 引入思源宋体或类似字体 */
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px; /* 导航栏高度 */
}

.logo a {
  color: #6a4a3a; /* 深棕色 */
  font-size: 2rem; /* 字体大小 */
  font-weight: bold;
  text-decoration: none;
  letter-spacing: 1px;
  font-family: 'KaiTi', serif; /* 楷体或宋体等文化字体 */
  transition: color 0.3s ease;
}

.logo a:hover {
  color: #8b5e3c; /* 悬停颜色 */
}

.nav-links {
  display: flex;
  gap: 15px; /* 导航项间距 */
  flex-grow: 1; /* 让导航链接占据多余空间，将认证部分推到右边 */
  justify-content: flex-start; /* 从左侧开始排列 */
}

.auth-section {
  display: flex;
  align-items: center;
  margin-left: 20px; /* 与导航链接的间距 */
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.username {
  color: #6a4a3a;
  font-size: 0.9rem;
  font-family: 'KaiTi', serif;
}

.auth-button {
  padding: 8px 12px;
  border: 1px solid #8b5e3c;
  background-color: transparent;
  color: #6a4a3a;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-family: 'KaiTi', serif;
  font-size: 0.9rem;
}

.auth-button:hover {
  background-color: #8b5e3c;
  color: #f8f0e3;
}

.logout-button {
  /* 可以添加特定于退出按钮的样式 */
}

.login-button {
  /* 可以添加特定于登录按钮的样式 */
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 8px 12px; /* 内边距 */
  color: #6a4a3a; /* 默认颜色 */
  text-decoration: none;
  border-radius: 4px; /* 圆角 */
  transition: all 0.3s ease;
  position: relative;
}

.nav-item::before {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 0; /* 默认宽度为0 */
  height: 2px; /* 下划线高度 */
  background-color: #8b5e3c; /* 下划线颜色 */
  transition: width 0.3s ease;
}

.nav-item:hover {
  color: #8b5e3c; /* 悬停颜色 */
  transform: translateY(-2px); /* 悬停上浮 */
}

.nav-item:hover::before {
  width: 100%; /* 悬停时宽度为100% */
}

.nav-item.active {
  color: #8b5e3c; /* 激活颜色 */
  font-weight: bold;
}

.nav-item.active::before {
  width: 100%; /* 激活时宽度为100% */
}

.nav-icon {
  font-size: 1.2rem; /* 图标大小 */
  margin-right: 5px; /* 图标与文字间距 */
}

.nav-text {
  font-size: 1rem; /* 文字大小 */
  font-family: 'KaiTi', serif; /* 文化字体 */
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-container {
    flex-direction: column;
    height: auto;
    padding: 10px 0;
  }
  
  .logo {
    margin-bottom: 10px;
  }
  
  .logo a {
    font-size: 1.8rem;
  }
  
  .nav-links {
    width: 100%;
    justify-content: space-around;
    gap: 10px;
    order: 2; /* 将导航链接放在认证部分下方 */
    margin-top: 10px;
  }
  
  .auth-section {
    order: 1; /* 将认证部分放在导航链接上方 */
    margin-left: 0;
    margin-bottom: 10px;
  }
  
  .nav-item {
    padding: 6px 10px;
  }
  
  .nav-icon {
    font-size: 1rem;
    margin-right: 3px;
  }
  
  .nav-text {
    font-size: 0.9rem;
  }
}

@media (max-width: 480px) {
  .logo a {
    font-size: 1.6rem;
  }
  
  .nav-links {
    flex-wrap: wrap; /* 换行 */
    justify-content: center;
  }
  
  .auth-section {
    width: 100%;
    justify-content: center;
  }

  .user-info {
    flex-direction: column; /* 在小屏幕上垂直排列用户名和按钮 */
    align-items: center;
    gap: 5px;
  }

  .auth-button {
    width: 100%;
    max-width: 150px; /* 按钮最大宽度 */
    text-align: center;
  }
  
  .nav-item {
    padding: 5px 8px;
  }
  
  .nav-icon {
    font-size: 0.9rem;
  }
  
  .nav-text {
    font-size: 0.8rem;
  }
}
</style>